
package com.microweb.toastmaster.dao.hibernate;

import java.util.List;
import com.microweb.toastmaster.model.Agenda;
import com.microweb.toastmaster.dao.AgendaDao;
import com.microweb.toastmaster.model.Query;
import java.util.ArrayList;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class AgendaDaoHibernate extends BaseDaoHibernate implements AgendaDao {

    /**
     * @see com.microweb.toastmaster.dao.AgendaDao#getAgendas(com.microweb.toastmaster.model.Agenda)
     */
    public List getAgendas(final Agenda agenda) {
        return getHibernateTemplate().find("from Agenda order by date desc");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (agenda == null) {
            return getHibernateTemplate().find("from Agenda");
        } else {
            // filter on properties set in the agenda
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(agenda).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(Agenda.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }
    

    /**
     * @see com.microweb.toastmaster.dao.AgendaDao#getAgenda(Long id)
     */
    public Agenda getAgenda(final Long id) {
        Agenda agenda = (Agenda) getHibernateTemplate().get(Agenda.class, id);
        if (agenda == null) {
            log.warn("uh oh, agenda with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(Agenda.class, id);
        }

        return agenda;
    }

    /**
     * @see com.microweb.toastmaster.dao.AgendaDao#saveAgenda(Agenda agenda)
     */    
    public void saveAgenda(final Agenda agenda) {
        getHibernateTemplate().saveOrUpdate(agenda);
    }

    /**
     * @see com.microweb.toastmaster.dao.AgendaDao#removeAgenda(Long id)
     */
    public void removeAgenda(final Long id) {
        getHibernateTemplate().delete(getAgenda(id));
    }
}
